package com.kgd.news.ibeetl.tag;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.kgd.news.utils.StringUtilsWx;
import org.beetl.core.Tag;

import java.util.List;
import java.util.Map;

/**
 * @Email: huangsanm@foxmail.com
 * @Date: 2017/8/3
 * @Time: 下午2:47
 * 动态的生成弹出的选择框的模版
 */
public class OneToManyDisplayTag extends Tag {

    @Override
    public void render() {
        try {
            Map attrs = (Map) args[1];
            String val = attrs.get("val") == null ? "0" : attrs.get("val") + "";
            Integer selVal = Integer.valueOf(StringUtilsWx.isEmpty(val)  ? "-1" : val);
            String pname = attrs.get("pname") + "";
            String defauleName = attrs.get("defaultNumber") == null ? "0" : attrs.get("defaultNumber") + "";
            int defaultNumber = Integer.valueOf(StringUtilsWx.isEmpty(defauleName) ? "-1" : defauleName);
            String tblName = attrs.get("table") + "";
            String columnName = attrs.get("columnName") + "";
            String whereSel = attrs.get("whereSel") + "";
            String whereArgs = attrs.get("whereArgs") + "";
            ctx.byteWriter.writeString(select(selVal, pname, tblName, columnName, whereSel, whereArgs, defaultNumber));
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

    /**
     * 下拉标签
     * @param selVal
     * @param defaultnumber
     * @return
     */
    private String select(int selVal, String pname, String tblName, String columnName, String whereSel, String whereArgs, int defaultnumber) {
        StringBuilder sb = new StringBuilder();
        sb.append("<select name=\"").append(pname).append("\" ");
        sb.append(" id=\"").append("smallapptype").append("\" >");
        sb.append("<option value=\"").append("").append("\">");
        sb.append("请选择");
        sb.append("</option>");

        String sql = "select " + columnName + ", id from " + tblName + " where 1=1";
        if (!StringUtilsWx.isEmpty(whereSel) && !StringUtilsWx.isEmpty(whereArgs)) {
            sql += " and " + whereSel + "=" +  whereArgs;
        }
        List<Record> list = Db.find(sql);

        for (Record r : list) {
            String name = r.get(columnName);
            long id = r.getLong("id");
            if ((selVal != 0 && selVal == id) || ((-1==selVal)&&id == Integer.valueOf(defaultnumber))) {// 默认选中
                sb.append("<option value=\"").append(id).append("\" selected=\"selected\">");
                sb.append(name);
                sb.append("</option>");
            } else {
                sb.append("<option value=\"").append(id).append("\">");
                sb.append(name);
                sb.append("</option>");
            }
        }
        sb.append("<option value=\"-1").append("").append("\">");
        sb.append("未分类");
        sb.append("</option>");
        sb.append("</select>");
//<select name="categoryid" ><option value="">请选择</option><option value="9">职场</option><option value="10">萝莉</option><option value="11">制服</option><option value="25">明星</option><option value="26">欧美</option><option value="27">日韩</option></select>
        return sb.toString();
    }
}
